//
//  SQAuthViewController.swift
//  SwiftWeibo
//
//  Created by 苏强 on 16/11/30.
//  Copyright © 2016年 suqianghotel. All rights reserved.
//

import UIKit
import WebKit
import Foundation
import SVProgressHUD


class SQAuthViewController: UIViewController {
    
    var client_id = "65802674"
    
    var redirect_uri = "http://my.dingdone.com/share/callback.php"
    
    var client_secret = "32905027b41f4854bd232ee469845b4a"
    
    var webView: WKWebView{
        return view as! WKWebView
    }
    
    override func loadView() {
        view = WKWebView()
        view.backgroundColor = UIColor.white
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.webView.navigationDelegate = self
        
        let rights = [UIBarButtonItem.space,
                      UIBarButtonItem(title: "关闭",
                                      colorHighlight: UIColor.gray,
                                      colorNormal :UIColor.orange,
                                      target: self,
                                      action: #selector(cancle)),
                      UIBarButtonItem(title: "自动填充",
                                      colorHighlight: UIColor.gray,
                                      colorNormal :UIColor.orange,
                                      target: self,
                                      action: #selector(autoFill))]
        
        
        
        self.navigationItem.rightBarButtonItems = rights
        self.title = "微博授权登陆页"
        
        let url = "https://api.weibo.com/oauth2/authorize?client_id=\(client_id)&redirect_uri=\(redirect_uri)"
        
        let URL = NSURL(string:url) as! URL
        
        
        let request = URLRequest(url: URL)
        
        
        self.webView.load(request)
        
        
        
    }
    
    @objc private func autoFill()
    {
        let js = "document.getElementById('userId').value = '1965834517@qq.com'; " +
        "document.getElementById('passwd').value = 'You$2o00';"
        self.webView.evaluateJavaScript(js, completionHandler: nil)
    }

 
    
    @objc private func cancle()
    {
        self.dismiss(animated: true, completion: {() in })
    }
    
    
    func close() -> Void {
        self.dismiss(animated: true, completion: {() in })
        SVProgressHUD.dismiss()
    }
    


}


extension SQAuthViewController : WKNavigationDelegate{
   
 
 
    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!)
    {
        SVProgressHUD.show()
    }

    
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!)
    {
        SVProgressHUD.dismiss()
        guard let url = webView.url else {

            return;
        }
        
        if url.host == "my.dingdone.com" && url.query!.contains("code=")
        {
        
            let code = (url.query! as NSString).substring(from: 5)
            
            accessToken(with: code)
            
        }

    
    }
    
    
    private func accessToken(with code:String)
    {
        
        SVProgressHUD.show()
        SQHttpManager.shared.loadAccessToken(with: code, completion: { (isSuccess) in
            
            if isSuccess
            {
                SVProgressHUD.setStatus("授权成功")
                
            }
            else
            {
                SVProgressHUD.setStatus("授权失败")
            }
            
            self.close()
            
        })

    }
    
    
    


}
